from apps.common.base.dao import BaseDao
from apps.merchant.genera.serializers import *
from apps.common.extension.customException import *
from apps.merchant.item.dao import ItemDao

class GeneraDao(BaseDao):
    MODEL_CLASS = Genera
    SERI_CLASS = GeneraBaseSerializer

    def delete(self, obj_ids, data=...):
        # 有下级不能删除
        try:
            for obj_id in obj_ids:
                if self.findAll().filter(parent_id=obj_id).count() > 0:
                    raise VerifyException('有下级不能删除')
                # 下面有商品不能删除
                if ItemDao().findAll().filter(genera_id=obj_id).count() > 0:
                    raise VerifyException('类目下有商品不能删除')
            return super().delete(obj_ids, data)
        except Exception as e:
            raise e